package com.ikun.yummy.sourceop.op;

import android.annotation.SuppressLint;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ikun.yummy.bean.FollowInfo;
import com.ikun.yummy.properties.AppProperties;
import com.ikun.yummy.sourceop.sqlutils.SqliteHelper;

import java.util.ArrayList;
import java.util.List;

public class FollowOP {
    private final SQLiteDatabase db;
    public FollowOP(Context context){
        db = new SqliteHelper(context).getReadableDatabase();
    }
    //给A添加粉丝B，就要给B添加关注A
    public void insertFollower(FollowInfo followInfo){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("username", followInfo.username);
        values.put("othername", followInfo.othername);
        db.insert(AppProperties.FOLLOWER_TABLE, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }
    public void insertFollowee(FollowInfo followInfo){
        db.beginTransaction();
        ContentValues values = new ContentValues();
        values.put("username", followInfo.username);
        values.put("othername", followInfo.othername);
        db.insert(AppProperties.FOLLOWEE_TABLE, null, values);
        db.setTransactionSuccessful();
        db.endTransaction();
    }

    public void deleteFollower(String followerid) {
        db.delete(AppProperties.FOLLOWER_TABLE, "followerid=?",new String[]{followerid});
    }
    public void deleteFollowee(String followeeid) {
        db.delete(AppProperties.FOLLOWEE_TABLE, "followeeid=?",new String[]{followeeid});
    }

    public List<FollowInfo> queryFollower(String username){//查询follower都相等的followee
        String sql="SELECT * FROM "+AppProperties.FOLLOWER_TABLE+" WHERE username='"+username+"'";
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<FollowInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new FollowInfo(cursor.getString(0),cursor.getString(1), cursor.getString(2),1));
            }
            return list;
        }else{
            return null;
        }
    }
    public List<FollowInfo> queryFollowee(String username){
        String sql="SELECT * FROM "+AppProperties.FOLLOWEE_TABLE+" WHERE username='"+username+"'";
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor != null && cursor.getCount() > 0) {
            ArrayList<FollowInfo> list = new ArrayList<>();
            while (cursor.moveToNext()){
                list.add(new FollowInfo(cursor.getString(0),cursor.getString(1), cursor.getString(2),1));
            }
            return list;
        }else{
            return null;
        }
    }
    //知道username和othername后就可以查询到一个人了，用来查询自己是不是对方的粉丝或者关注
    public FollowInfo queryFollowerSingle(String username,String othername){
        String sql="SELECT * FROM "+AppProperties.FOLLOWER_TABLE+" WHERE username='"+username+"'"+" AND othername='"+othername+"'";
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                return new FollowInfo(cursor.getString(0),cursor.getString(1), cursor.getString(2),1);
            }
            return null;
        }else{
            return null;
        }
    }
    public FollowInfo queryFolloweeSingle(String username, String othername) {
        String sql="SELECT * FROM "+AppProperties.FOLLOWEE_TABLE+" WHERE username='"+username+"'"+" AND othername='"+othername+"'";
        @SuppressLint("Recycle") Cursor cursor =db.rawQuery(sql,null,null);
        if (cursor != null && cursor.getCount() > 0) {
            while (cursor.moveToNext()){
                return new FollowInfo(cursor.getString(0),cursor.getString(1), cursor.getString(2),1);
            }
            return null;
        }else{
            return null;
        }
    }
}